﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication9
{

    public partial class Form1 : Form
    {
        DS_Net_Client ds_net = new DS_Net_Client();
        List<Person> lst = new List<Person>();
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonCreate_Click(object sender, EventArgs e)
        {
            try
            {
                Person p = new Person();
                p.FName = textBox_FName.Text;
                p.LName = textBox_LName.Text;
                p.Age = Convert.ToInt32(textBox_Age.Text);
                ds_net.Create(p);
                lst = ds_net.Read();
                ShowList(lst);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "EXEPTION");
            }
        }

        private void buttonRead_Click(object sender, EventArgs e)
        {
            try{
            lst = ds_net.Read();
            ShowList(lst);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "EXEPTION");
            }
        }

        
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            try{
            ds_net.Delete(Convert.ToInt32(textBox_id.Text));
            lst = ds_net.Read();
            ShowList(lst);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "EXEPTION");
            }
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            try{
            Person p = new Person();
            p.FName = textBox_FName.Text;
            p.LName = textBox_LName.Text;
            p.Age = Convert.ToInt32(textBox_Age.Text);
            ds_net.Update(Convert.ToInt32(textBox_id.Text), p);
            lst = ds_net.Read();
            ShowList(lst);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "EXEPTION");
            }
        }

        private void ShowList(List<Person> list)
        {
            listView1.Items.Clear();
            foreach (Person a in list)
            {
                ListViewItem item = new ListViewItem(Convert.ToString(a.Id));
                item.SubItems.Add(a.FName);
                item.SubItems.Add(a.LName);
                item.SubItems.Add(Convert.ToString(a.Age));
                listView1.Items.Add(item);

            }
        }

        private void buttonSendFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "*.*|*.*";
            DialogResult openResult = openFileDialog1.ShowDialog();
            try
            {
                string fileName = openFileDialog1.FileName;
                ds_net.SendFile(fileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("EXCEPTION: " + ex.Message);
            }
        }

    }
}
